package pers.linkong.asr.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import pers.linkong.asr.domain.StudentBean;
import pers.linkong.asr.service.StudentService;
import pers.linkong.asr.util.LayPage;
import pers.linkong.asr.util.ResultInfo;

@Controller
@RequestMapping("/student")
public class StudentController {
	
	@Autowired
	private StudentService studentService;
	
	@RequestMapping("/add")
	@ResponseBody
	public ResultInfo addStudent(StudentBean student) throws IOException{
		try{
			this.studentService.addStudent(student);
			return ResultInfo.data(1, "保存成功");
		}
		catch(Exception e){
			e.printStackTrace();
			return ResultInfo.data(0, "发生异常，保存失败，请稍后再试");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public ResultInfo updateStudent(StudentBean student) throws IOException{
		try{
			System.out.println(student.getSid());
			this.studentService.updateStudent(student);
			return ResultInfo.data(1, "保存成功");
		}
		catch(Exception e){
			e.printStackTrace();
			return ResultInfo.data(0, "发生异常，保存失败，请稍后再试");
		}
	}
	
	// 分页查询学生
	@RequestMapping("/findStudentByPage")
	@ResponseBody
	public LayPage findStudentByPage(String sid, String sname, String ssex,
			String sclass, String grade, String major, String xueyuan,
			long page, long limit,  HttpSession session) throws Exception{
		
		StudentBean student = new StudentBean();
		long start;
		
		// 每页显示的记录条数，默认12条
		start = (page - 1) * limit;	

		// trim方法可以删除字符串前端和后端的空格（含tab键产生的空格）
		if(sid != null && sid != ""){
			student.setSid(Integer.parseInt(sid.trim()));
		}
		if(sname != null && sname != ""){
			student.setSname(sname.trim());
		}
		if(sclass != null && sclass != ""){
			student.setSclass(sclass.trim());
		} else {
			student.setSclass((String) session.getAttribute("sessionClass"));
		}
		if(ssex != null && ssex != ""){
			student.setSsex(ssex.trim());
		}
		if(grade != null && grade != ""){
			student.setGrade(grade.trim());
		}
		if(major != null && major != ""){
			student.setMajor(major.trim());
		}
		if(xueyuan != null && xueyuan != ""){
			student.setXueyuan(xueyuan.trim());
		}
		List<StudentBean> students
			= this.studentService.findStudentByPage(student, start, limit);
		long count = this.studentService.getStudentCount(student);
		
		System.out.println("SC:"+count);
		return LayPage.data(0, "", count, students);
	}
	
	// 根据id查询
	@RequestMapping("/getStudentById")
	@ResponseBody
	public StudentBean getStudentById(int sid) throws Exception{
		StudentBean student = this.studentService.getStudentById(sid);
		System.out.println(sid);
		student.setImgidlist(this.studentService.getImageById(sid));
		return student;
	}
	
	// 根据id删除学生
	@RequestMapping("/deleteStudentById")
	@ResponseBody
	public ResultInfo deleteStudentById(int sid) throws IOException{
		try{
			this.studentService.deleteStudent(sid);
			return ResultInfo.data(1, "删除成功");
		}
		catch(Exception e){
			e.printStackTrace();
			return ResultInfo.data(0, "删除失败，请先删除该生所有考勤记录");
		}
	}

}